package com.itheima.hmshopping.app.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.PmsProductApi;
import com.itheima.hmshopping.dubbo.api.SmsFlashPromotionProductRelationApi;
import com.itheima.hmshopping.entity.PmsProduct;
import com.itheima.hmshopping.entity.SmsFlashPromotion;
import com.itheima.hmshopping.entity.SmsFlashPromotionProductRelation;
import com.itheima.hmshopping.util.HmPage;
import com.itheima.hmshopping.vo.PmsProVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SmsFlashPromotionProductRelationService {
    @DubboReference
    private SmsFlashPromotionProductRelationApi productRelationApi;
    @DubboReference
    private PmsProductApi pmsProductApi;

    /**
     * 批量选择商品添加关联
     * @param list
     */
    public void create(List<SmsFlashPromotionProductRelation> list) {
        productRelationApi.create(list);
    }

    /**
     * 删除关联
     * @param id
     */
    public void delete(Integer id) {
        productRelationApi.delete(id);
    }

    /**
     * 分页查询不同场次关联及商品信息
     * @param flashPromotionId
     * @param flashPromotionSessionId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public HmPage list(Integer flashPromotionId, Integer flashPromotionSessionId, Integer pageNum,Integer pageSize) {
        //从关系表查询出ids和sort
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = new SmsFlashPromotionProductRelation();
        //对id进行赋值
        smsFlashPromotionProductRelation.setFlashPromotionId(Convert.toLong(flashPromotionId));
        smsFlashPromotionProductRelation.setFlashPromotionSessionId(Convert.toLong(flashPromotionSessionId));
        //调用方法进行查询
        Page list = productRelationApi.list(smsFlashPromotionProductRelation, pageNum, pageSize);
        List<SmsFlashPromotionProductRelation> records = list.getRecords();
        //获取id的集合
        List<Long> productIds = CollUtil.getFieldValues(records, "productId", Long.class);
        List<PmsProVo> pmsProductVos = new ArrayList<>();
        //根据productIds获得所有的产品
        Map<Long, PmsProduct> productMap = pmsProductApi.getProductByIds(productIds);
        for (SmsFlashPromotionProductRelation record : records) {
            PmsProduct pmsProduct = productMap.get(record.getProductId());
            if (pmsProduct != null) {
                PmsProVo pmsProductVo = new PmsProVo();
                pmsProductVo.setProduct(pmsProduct);
                BeanUtils.copyProperties(record,pmsProductVo);
                pmsProductVos.add(pmsProductVo);
            }
        }

        HmPage hmPage=new HmPage();
        //设置总页数
        hmPage.setTotalPage(Convert.toInt(list.getPages()));
        //设置数据
        hmPage.setList(pmsProductVos);
        //第几页
        hmPage.setPageNum(Convert.toInt(pageNum));
        //总页数
        hmPage.setTotal(Convert.toInt(list.getTotal()));
        //每页条数
        hmPage.setPageSize(pageSize);
        return hmPage;

    }

    /**
     * 修改关联信息
     * @param id
     * @param relation
     */
    public void update(Long id, SmsFlashPromotionProductRelation relation) {
        productRelationApi.update(id,relation);
    }

    /**
     * 获取场次详情
     * @param id
     * @return
     */
    public SmsFlashPromotionProductRelation getOne(Long id) {
        return productRelationApi.getOne(id);
    }
}
